var menuTree;
var username=localStorage.getItem("username");
var token=localStorage.getItem("token");
var userid=localStorage.getItem("userid");
var sysinfoID;
var sysinfoList;
var isFirst=0;
var productData;
tl_config.init_url='welcome.do';
if(username==null||token==null||userid==null){
	localStorage.clear();
	window.location.href="login.do";
}
$(function(){
	
	$("#userName").text(username);
	
	//获取当前系统id
	$.ajax({
 		type : "get",
 		url : server_url+'/rest/last/login/select.do?staffId='+userid+'&ran='+Math.random(),
 		dataType:'json',
 		async : false,
 		data:{
 			token:token
 		},
 		success : function(result) {
 			if (result.retCode == '0000') {
// 				console.debug(result.data.sysinfoId);
 				sysinfoID=result.data.sysinfoId;
 				localStorage.setItem("sysinfoID",sysinfoID);
 			}else{
 				localStorage.clear();
 				window.location.href="login.do";
 			}
 		   }
 	});
	//获取系统列表
	$.ajax({
 		type : "get",
 		url : server_url+'/rest/sysInfoList.do?ran='+Math.random(),
 		dataType:'json',
 		async : false,
 		data:{
 			token:token
 		},
 		success : function(result) {
 			if (result.retCode == '0000') {
 				sysinfoList=result.data.rows;
 			}
 			else{
 				localStorage.clear();
 				window.location.href="login.do";
 			}
 		   }
 	});
	
	//变化系统
	swapSys(sysinfoID);
	
	
});

function swapSys(sysInfo_id){
	localStorage.setItem("sysinfoID",sysInfo_id);
	if(username=='admin'){
		$.ajax({
	 		type : "get",
	 		url : server_url+'rest/menu/menuList.do?sysId='+sysInfo_id+'&ran='+Math.random(),
	 		dataType:'json',
	 		async : false,
	 		data:{
	 			token:token
	 		},
	 		success : function(result) {
//	 		    console.debug(result);
	 		   if(result.retCode == '1003'){
	 			  localStorage.clear();
	 			  window.location.href="login.do";
	 			  return;
	 		   }
	 			if (result.retCode == '0000') {
	 				menuTree=result.data;
	 				/*$(".g_daoNav").find("ul").addClass("");
	 				console.log($(".g_bifUlbao").c.length);
	 				console.log(result.data[2]);*/
	 			}
	 			else{
	 				menuTree="";
	 			}
	 			
	 			
	 			tl_config.reloadMenu(menuTree);
 				modifySysInfoText(sysInfo_id);
	 		 }
	 	});
	}else{
		$.ajax({
			type : "get",
			url : server_url+'rest/menu/menuListByStaff.do?ran='+Math.random(),
			dataType:'json',
			async : false,
			data:{
				staff_id:userid,
				sysinfo_id:sysInfo_id,
				token:token
	 		},
			success : function(result) {
				isFirst+=1;
//				console.debug(result);
				if(result.retCode == '1003'){
		 			  localStorage.clear();
		 			  window.location.href="login.do";
		 			  return;
		 		 }
				
	 			if (result.retCode == '0000') {
	 				menuTree=result.data;
	 			}
	 			else{
	 				menuTree="";
	 			}
	 			tl_config.reloadMenu(menuTree);
 				modifySysInfoText(sysInfo_id);
	 			
			   }
			});
	}
	
	
}

function modifySysInfoText(sysInfo_id){
	sysinfoID=sysInfo_id;
	$("#sysInfoList").html("");
	for(var i=0;i<sysinfoList.length;i++){
		var sysInfoid=sysinfoList[i].id;
//		console.log(sysInfoid);
		if(sysInfoid==sysInfo_id){
			//$("#current_sysinfo").text(sysinfoList[i].sysName);
			$("#current_sysinfo").text("那山那茶后台");
		}
		else{
			var sysText="<li><a href='javascript:void(0);' onclick=swapSys('"+sysInfoid+"') >"+sysinfoList[i].sysName+"</a></li>";
			$("#sysInfoList").append(sysText);
		}
	}
	
	//更新最近登录系统
	lastLoginSysInfoUpd();
}

function lastLoginSysInfoUpd(){
	$.ajax({
 		type : "get",
 		url : server_url+'/rest/last/login/update.do?ran='+Math.random(),
 		dataType:'json',
 		data:{
 			staffId:userid,
 			sysinfoId:sysinfoID,
 			token:token
 		},
 		async : false,
 		success : function(result) {
// 		    console.debug(result);
 			if (result.retCode == '0000') {				
 			}
 			else{
 				localStorage.clear();
 				window.location.href="login.do";
 			}
 		   }
 	});
}

function loginOut(){
	localStorage.clear();
	window.location.href="login.do";
}

window.onbeforeunload = function(event){
	//localStorage.clear();	
};
		